package info.itloser.learnkotlindemo

import android.content.Context
import android.content.Intent
import android.os.*
import android.view.View
import android.view.WindowInsets
import android.view.WindowInsetsController
import androidx.appcompat.app.AppCompatActivity

/**
 * 启动页
 */
class SplashActivity : AppCompatActivity() {

    val context: Context = this

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        //  注意 API-30 systemUiVisibility 已经不推荐使用
        //  请使用 windowInsetsController 我TM人傻了 这玩意只能在API-30也就是Android11的手机上使用 10及以下会报错 找不到相关函数
        //  https://blog.csdn.net/jingzz1/article/details/111468726

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

            //获取 windowInsetsController 实例
            val windowController = window.insetsController

            //设置 状态栏 主题为 黑色
            windowController?.setSystemBarsAppearance(
                WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
                WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
            )

            //自动隐藏导航栏（在不触发手势时）
            windowController?.systemBarsBehavior =
                WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE

            //隐藏导航栏
            windowController?.hide(WindowInsets.Type.navigationBars())

        } else {

            //状态栏黑色+全屏
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                window.decorView.systemUiVisibility =
                    (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)

            }

        }

        //延时启动
        handler.sendEmptyMessageDelayed(1, 600)

    }

    private val handler: Handler = object : Handler(Looper.getMainLooper()) {

        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)

            when (msg.what) {
                1 -> {

                    //打开首页
                    val intent2Main = Intent(context, MainActivity::class.java)
                    startActivity(intent2Main)
                    finish()

                }
                else -> {


                }
            }
        }

    }


}